<include file="Common:header" />
<body>
<table><tr >
<td width="160" valign="top" bgcolor="#eaeaea"><include file="Common:adminpanel" /></td>

<td valign="top" >
<div>

<h1>待发货订单</h1>
<!-- 
<form method="post" id="tradeSearch" name="tradeSearch"  action="<{:U('tsearch')}>">
<table width="100%" cellspacing="1" cellpadding="3" border="0">
<tr>
	<td>搜索订单</td>
	<td>
		<select name="field" id="field">
		<option value="receiver_name">收货人姓名</option>
		<option value="buyer_nick">买家昵称</option>
		<option value="receiver_mobile">收货人的手机号码</option> 	
		<option value="receiver_phone">收货人的电话号码</option>
		<option value="tplid">快递模板</option>
		</select>
		
		<input type="text" id="k" name="k" value="<{$map[k]}>">
		<select name="tplid" id="tplid" style="display:none"><option value="0">选择快递</option><{$tploptions}></select>
		
		<input type="hidden" name="status" id="status" value="<{$status}>" />
		<input type="submit" name="submit" id="submit" value="搜索" />
	</td>
	<td>
		
		
		
		
		
	</td>
</tr>
</table>
</form>
 -->
 
 <table width="100%" cellspacing="1" cellpadding="3" border="0">
<tr>
	<td><div>按快递筛选:
		<select name="tplid" id="tplid"><option value="<{:U('Trade/tlist?status=1')}>">选择快递</option><{$tplsearch}></select>
		</div>	
	</td>
	<td>
		
	</td>
	<td>
	</td>
</tr>
</table>

<form id="form1" name="form1" method="post" action="<{:U('tsidsave')}>">
<table width="100%" cellspacing="1" cellpadding="0" border="0">
<tr  class="header">
<th width="50" height="30" align="center">全选</th>
<th width="100">订单号</th>
<th>卖家帐号</th>
<th>买家帐号</th>
<th>收货地址</th>
<th width="10%">卖家备注<br>
  买家留言</th>
<th width="130">商品信息</th>
<th width="80" align="center">快递</th>
<th>订单金额<br>
  总成本</th>
<th>运单号</th>
<th width="50" align="center">打印</th>
</tr>
<php>
if( $tradelist ) {
	
	foreach( $tradelist as $trade ) {
		$ordersinfo = '';
		$orders = unserialize( $trade['orders'] );		
		foreach(  $orders['order'] as $t=>$order ){
			$ordersinfo .= '<div>'. ($t+1).'.'. $order['outer_sku_id'] .'('.$order['num'] .'件)</div>';
		}
		$company = D('Company')->where("id={$trade['syscomid']}")->find();
		$tprint = U("tprint?tid={$trade['tid']}");
		$tradeflag = tradeflag( $trade['seller_flag'] );
		
		echo '<tr class="row">
		<td height="50" align="center"><input type="checkbox" name="tid[]" id="tid_'. $trade['tid']. '" value="'. $trade['tid']. '" /></td>
		<td>'. $trade['tid']. '</td>
		<td>'. $trade['seller_nick'] . '</td>
		<td>'. $trade['buyer_nick'] .'<br>'. contacttuser( $trade['buyer_nick'] ) . '</td>
		<td>'. $trade['receiver_name'] .','. $trade['receiver_mobile'] . ','. $trade['receiver_phone'] .',' . $trade['receiver_state'] .', '. $trade['receiver_city']. ' '. $trade['receiver_district'] .','. $trade['receiver_address'] .','. $trade['receiver_zip'] . '</td>
		<td align="center"><span style="color:red">'. $tradeflag . ' '. $trade['seller_memo'] . '</span><br><span style="color:blue">' . $trade['buyer_message'] .'</span></td>
		<td>'. $ordersinfo .'</td>
		<td align="center"><div id="editCom_'. $trade['tid'] .'" style="cursor:pointer;" alt="'. $trade['tid'] .'">'. $company['name'] .' </div></td>
		<td>' .$trade['payment']. '/'. $trade['syscost'] .' </td>
		<td><input name="out_sid[]" type="text" id="out_sid_'.$trade['tid'].'" value="" size="15"></td>
		<td width="50" align="center">[<a href="'. $tprint. '" target="_blank">打印</a>] - <input type="button" id="btnOneOutSidSave_' .$trade['tid']. '" value="发货" alt="' .$trade['tid']. '"> <input type="hidden" value="'.$company['code'].'" id="company_code_'.$trade['tid'].'" name="company_code[]">
			<input type="hidden" value="'.$trade['tid'].'" name="listtid[]">
		</td></tr>';
	} 
}
//<input type="button" id="btnTradeSend_'.$trade['tid'].'" alt="'.$trade['tid'].'" value="发货" />
</php>
<tr class="row">
<td colspan="11" height="60">

	<input type="checkbox" name="tidall" id="tidall"><label for="tidall">全选</label>
	&nbsp;&nbsp;&nbsp;
	<span id="formPrint">
	<input type="button" name="btnPrint" id="btnPrint" value="批量打印订单" />
	<input type="hidden" name="tidlist" id="tidlist" value="" >
	</span>
	&nbsp;&nbsp;&nbsp;
	<select name="company" id="company"><option value="0">选择快递</option><{$tploptions}></select>
	<label for="btnSidStart" >运单号增量</label><input type="text" name="btnSidIncre" id="btnSidIncre" value="1" style="height:30px;width:30px;padding:2px;font-size:14px;line-height:30px; color:blue;" />
	
	<label for="btnSidStart" >起始运单号</label><input type="text" name="btnSidStart" id="btnSidStart" value="" style="height:30px;width:200px;padding:2px;font-size:14px;line-height:30px; color:blue;" />
	<input type="button" name="btnOutSid" id="btnOutSid" value="批量填写运单号" />
	<input type="submit" name="btnOutSidSave" id="btnOutSidSave" value=" 批量保存运单号&批量发货 " />
	<span style="font-family: 微软雅黑, Tahoma; letter-spacing: 2px; display: none;" id="result"></span>
</td>

<td ></td>
</tr>
<tr class="row">
<td colspan="11" height="30" align="center"><div><{$page}></div></td>
</tr>
</table>
</form>
</div>
</td>
</tr>
</table>

<script>
$(document).ready(function(){
	$("select[id^='company_']").bind('change', function(){
		var tplid = $(this).val();
		var tid   = $(this).attr( 'alt' );
		$.post ( '<{:U('Index/price')}>', { trade : $("#tradedata_" + tid ).val() , tplid: tplid }  ,
			function(data){
				if( 1 == data['status'] ) {
					$('#chengben_'+tid).html( data['data']['cost'] );	
				} else {
					alert( data['info']);
				}
			 	
			}	
		); 
	});
	
	
	
	$("input[id^='btnTradeSend']").bind('click', function(){
		var tid = $(this).attr( 'alt' );
		var company_code = $( '#company_code_'+tid ).val();
		var out_sid = $( '#out_sid_'+tid ).val();
		if( out_sid == null || '' == out_sid ) {
			alert('填写运单号');
			$( '#out_sid_'+tid ).focus();
			return false ;
		}
		$.post( '<{:U('Trade/tsend')}>' , {
				tid : tid, company_code: company_code ,  out_sid : out_sid 
			}, 
			function(data){
				if( 1 == data['status'] ){
					alert( data['info'] );
				}else {
					alert('提交失败 '+ data['info'] );	
				}
				
			} 
		);
	} );
	
	
	//搜索
	/*
	$("#field").val( '<{$map[field]}>' );
	if(  $("#field").val() == 'tplid' ){
		$("#tplid").show();	
		$("#k").hide();
		
		$("#tplid").val( <{$map[tplid]}> );
	}else {
		$("#tplid").hide();
		$("#k").show();
	}
	
	$("#field").bind("change", function(){
		if( $(this).val() == 'tplid' ){
			$("#tplid").show();	
			$("#k").hide();
		}else {
			$("#tplid").hide();
			$("#k").show();
		}
		
		
	});
	*/
	
	//保存单个运单号
	$("input[id^='btnOneOutSidSave_']").bind( 'click', function(){
		var error = '';
		var _this = $(this);
		var tid = _this.attr('alt');
		
		
		var objsid = $("#out_sid_" + tid );
		var out_sid = objsid.val(  );
		var company_code = $("#company_code_"+ tid ).val();
		
		if( out_sid == "" ) {
			objsid.css( 'background' ,'red');
			error +=  tid + "单号未填写\r\n" ;
			
		}

		if( error != '' ) {
			alert( error );
			return false;
		}else{
			_this.attr( "disabled" , "disabled").val("正在发货");
		}
		$.post( '<{:U('tsidsaveone')}>',  {
			out_sid 		: out_sid,
			tid				: tid
			
		}, function(data){
			if (data.status == 1) {
				
				_this.val("已发货");
				
			} else {
				alert(data.info);
				_this.removeAttr( "disabled" ).val("发货");
			}
			
			
		}, 'json');


		
	});
	
	//筛选
	
	$("#tplid").bind( 'change', function(){
		var url =  $(this).val();
		window.location.href= url;
		
	} );
	
	//打印相关
	$("#btnPrint").click( function() {
		var _this = $(this);
		_this.attr( "disabled" , "disabled").val("正在批量打印");
		var tids = $(":input[id^='tid_']:checked");
		var error  = '';
		var tidstr = '';
		if( tids.length == 0 ) { 
			error = '订单未选择';
		} else { 
			tids.each( function( i ) {
				var tid = $(this).val();
				tidstr += tid+';';
				
			} );
			
		}
		
		if( error != '' ) {
			alert( error );
			return false;
		}else{
			$("#tidlist").val( tidstr );
			var f = document.createElement("form");
			f.id	 = "formPrint";
			f.action = "<{:U('Trade/tprintall')}>";
			f.target = "_blank";
			f.method = "post";
			f.innerHTML = document.getElementById( 'formPrint' ).innerHTML ;
			document.body.appendChild( f );
			f.submit();
			
		}
		setTimeout( function(){ 
			_this.removeAttr( "disabled" ).val("批量打印订单");
			}, 5000
		);
		
		
	} );
	
	//全选
	
	$(":checkbox[id^=tid_]").click( function() {
		chcekstyle( $(this) ); 
		
	} );
	function chcekstyle ( $checkbox ) {
		var tr = $checkbox.parent().parent();
		var checked = $checkbox.attr("checked");
		if( checked ) {
			tr.css('background', '#CCFFCC');
		}else {
			tr.css('background', '');
		}
		
	}
	
	$("#tidall").click( function () {
		$(":checkbox[id^=tid_]").attr("checked", $(this).attr("checked") );
		$(":checkbox[id^=tid_]").each( function( i ){
			chcekstyle( $(this) ); 
			
		} );
	} );
	

	//批量填写运单号
	$("#btnOutSid").click( function(){ 
		var startsid = $.trim($("#btnSidStart").val());
		var sidincre = $.trim( $("#btnSidIncre").val() );
		if( startsid =="" ){
			$("#btnSidStart").focus();
		}else{
			var currentsid = startsid;
			$(":input[id^='tid_']:checked").each( function( i ) {
				var tid = $(this).val();
				var objsid = $("#out_sid_" + tid );
				objsid.val( currentsid );
				currentsid = parseInt( currentsid) +   parseInt(sidincre );
				
			} );
		}
		
	} );
	
	//批量保存运单号
	/*
	$("#btnOutSidSave").click( function() {
		
		var error = '';
		$(":input[id^='tid_']:checked").each( function( i ) {
			var tid = $(this).val();
			var objsid = $("#out_sid_" + tid );
			var out_sid = objsid.val(  );
			if( out_sid == "" ) {
				objsid.css( 'background' ,'red');
				error +=  tid + "单号未填写\r\n" ;
				
			}
		} );
		if( error != '' ) {
			alert( error );
			return false;
		}else {
			$(":input[id^='tid_']:checked").each( function( i ) {
				var tid = $(this).val();
				var objsid = $("#out_sid_" + tid );
				var out_sid = objsid.val(  );
				
			} );
			$.post( '<{:U}>', {} , function( data ) {} );
		}
		
		
		
	} );
	*/
	
	
	$('#form1').ajaxForm({
		beforeSubmit : checkForm, // pre-submit callback
		success : complete, // post-submit callback
		dataType : 'json'
	});
	function checkForm() {
		
		var error = '';
		var tids = $(":input[id^='tid_']:checked");
		if( tids.length == 0 ) { 
			error = '订单未选择';
		} else { 
			tids.each( function( i ) {
				var tid = $(this).val();
				var objsid = $("#out_sid_" + tid );
				var out_sid = objsid.val(  );
				if( out_sid == "" ) {
					objsid.css( 'background' ,'red');
					error +=  tid + "单号未填写\r\n" ;
					
				}
			} );
		
		}
		if( error != '' ) {
			alert( error );
			return false;
		}else{
			$("#btnOutSidSave").attr( "disabled" , "disabled").val("正在保存运单号...");
		}
		//可以在此添加其它判断	
	}
	function complete(data) {
		if (data.status == 1) {
			$('#result').html(data.info).show().fadeOut(2000);
			
		} else {
			$('#result').html(data.info).show();
		}
		$("#btnOutSidSave").removeAttr( "disabled" ).val("批量保存运单号");
	}
	
	
	
	
	//修改快递公司
	function comSlt ( $tid ) {
		var comSlt = "<select name='company' id='comSlt_"+ $tid +"'><{$tploptions}></select>";
			comSlt += '<input type="button" value="修改" id="btnUpCom_' + $tid + '" alt="' + $tid + '">';
		return comSlt;
	}
	
	//
	$("div[id^='editCom_']").bind( 'dblclick', function () {
		var _this = $(this);
		var tid = _this.attr('alt');
		_this.html( comSlt( tid ) );
	} );
	
	$("input[id^='btnUpCom_']").live( 'click', function(){
		var _this = $(this);
		try{ 
			var tid = _this.attr( 'alt' );
			var com = $("select#comSlt_" + tid +" :selected").text( );
			var systplid = $("select#comSlt_" + tid ).val( );
			if( tid ) {
				 $.post( '<{:U('Trade/tcompany')}>' , { tid:tid, systplid: systplid } , function(data) {
					 if( 1 == data['status'] ){
					 	$("div#editCom_" + tid ). html( com );
					 }
				 } );
			}
		} catch( e ) { }
		
		
		
		
	});
	
});
</script>
</body>
</html>